package com.shuwen.gcdj.controller.sysDictionary;


import com.shuwen.gcdj.bean.sysDictionary.request.*;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.sysDictionary.SysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/***
 ** Created YangX
 ** Date:2023/5/10
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/sysDictionary")
@Slf4j
public class SysDictionaryController extends AuthController {

    @Autowired
    private SysDictionaryService sysDictionaryService;

    /**
     * 获取字典项
     * @return
     */
    @ApiOperation(value = "获取字典项")
    @PostMapping(value = "/getParentDictionary")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SysDictionaryResp>> getParentDictionary(@Valid @RequestBody ParentDictionaryReq req)
    {
        List<SysDictionaryResp> result = new ArrayList<>();
        SysDictionaryReq serviceReq = new SysDictionaryReq();
        serviceReq.setName(req.getParentCode());
        result = sysDictionaryService.getListParentCode(serviceReq);
        return CommonResult.success(result);
    }

    /**
     * 获取字典明细信息
     * @return
     */
    @ApiOperation(value = "获取字典明细信息")
    @PostMapping(value = "/getChildDictionaryPage")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<SysDictionaryResp>> getChildDictionaryPage(@Valid @RequestBody SysDictionaryChildReq req)
    {
        return sysDictionaryService.getChildPage(req);
    }

    /**
     * 新增数据字典明细
     * @return
     */
    @ApiOperation(value = "新增数据字典明细")
    @PostMapping(value = "/addDetail")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> addDetail(HttpServletRequest request, @Valid @RequestBody AddSysDictionaryReq req)
    {
        InitUserDto initUserDto = initUser(request);
        return sysDictionaryService.addDetail(initUserDto,req);
    }

    /**
     * 编辑数据字典明细
     * @return
     */
    @ApiOperation(value = "编辑数据字典明细")
    @PostMapping(value = "/updateDetail")
    @ResponseBody
    @JWTAuth
    public CommonResult<Boolean> updateDetail(HttpServletRequest request, @Valid @RequestBody UpdateSysDictionartReq req)
    {
        InitUserDto initUserDto = initUser(request);
        return sysDictionaryService.updateDetail(initUserDto,req);
    }

    /**
     * 批量删除字典明细
     * @return
     */
    @ApiOperation(value = "批量删除字典明细")
    @PostMapping(value = "/batchDeleteDetail")
    @ResponseBody
    @JWTAuth
    public CommonResult<Boolean> batchDeleteDetail(HttpServletRequest request, @Valid @RequestBody DeleteSysdictionaryReq req)
    {
        InitUserDto initUserDto = initUser(request);
        return sysDictionaryService.batchDeleteDetail(initUserDto,req);
    }
}
